﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Configuration;

namespace BHair.Base
{
    public partial class frmkaiban : Form
    {
        public frmkaiban()
        {
            InitializeComponent();
        }
        string banzu="";
        int ifkaiban = 0;
        string constr = ConfigurationManager.ConnectionStrings["BHair.Properties.Settings.BHairDBConnectionString"].ConnectionString;
        private void button1_Click(object sender, EventArgs e)
        {
            //生成班组号
            if (txtjieling.Text != string.Empty)
            {
                shengcheng();
                selectkaiban();
            }
            else
            {
                MessageBox.Show("请输入借零金额");
            }
        }
        public void shengcheng()
        {
            //按日期生成班组码
            string time = DateTime.Now.ToString("yyyyMMdd");
            //MessageBox.Show(time);
            Random ran = new Random();
            int RandKey = ran.Next(10, 999);
            banzu = time + RandKey.ToString();
            // MessageBox.Show(banzu );
        }
        //查询用户是否已经开班
        public void selectkaiban()
        {
            string sql =string.Format( "SELECT [开班] FROM [BHairDB].[dbo].[LoginUser] where LoginUserID='{0}'",chuanzhi.userID);
            SqlConnection conn = new SqlConnection(constr);
            conn.Open();
            SqlCommand comm = new SqlCommand(sql,conn);
            SqlDataReader sdr = comm.ExecuteReader();
            while (sdr.Read())
            {
                ifkaiban =int.Parse( sdr["开班"].ToString());
            }
            if (ifkaiban == 0)
            {
                // MessageBox.Show("为开班");
                updatebanzu();
            }
            else
            {
                MessageBox.Show("已开班,请先交班！");
            }
            conn.Close();

        }
        /// <summary>
        /// 修改班组
        /// </summary>
        public void updatebanzu()
        {
            string sql =string.Format( "UPDATE [BHairDB].[dbo].[LoginUser] SET [开班] ={0},[班别] = '{1}',[借零]='{2}',[开班时间]='{3}' WHERE LoginUserID='{4}'",1,banzu,txtjieling.Text, DateTime.Now,chuanzhi.userID);
            try
            {
                SqlConnection conn = new SqlConnection(constr);
                conn.Open();
                SqlCommand comm = new SqlCommand(sql, conn);
                comm.ExecuteNonQuery();
                chuanzhi.banzu = banzu;
                MessageBox.Show("开班成功");
                conn.Close();
                this.Close();
            }
            catch (Exception ex) { MessageBox.Show(ex.Message); }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
